package com.bdzt.beidoubox.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Gallery;
import android.widget.ImageView;

import com.bdzt.beidoubox.R;
import com.bdzt.beidoubox.adapter.ScanResultAdapter;
import com.bdzt.beidoubox.entity.ScanBoxResultBean;
import com.bdzt.beidoubox.utils.OtherUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 、默 on 3/28/2017.
 */

public class SearchBoxFragment extends Fragment {
    private ImageView iv_scan;
    private Gallery vp_scanResult;
    private List<ScanBoxResultBean> list;
    private ScanResultAdapter adapter;
    private Context context;
    private boolean isStartAni = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        initView(view);
        return view;
    }

    private void initData() {
        list.clear();
        for(int i=1;i<=10;i++){
            ScanBoxResultBean bean = new ScanBoxResultBean();
            bean.setDate(OtherUtils.getDateTime());
            bean.setTitle("北斗盒子"+i);
            bean.setDirection("队友方位：西北");
            bean.setDistance("距离:1022km");
            bean.setLongitude("经度:20"+i+"°4"+i+"′55″");
            bean.setLatitude("纬度:12"+i+"°0"+i+"′11″");

            list.add(bean);
        }
        adapter.notifyDataSetChanged();

    }

    private void initView(View view) {
        context = getActivity();
        list = new ArrayList<>();

        iv_scan = (ImageView) view.findViewById(R.id.iv_scan);
        vp_scanResult = (Gallery) view.findViewById(R.id.vp_scanResult);
        adapter = new ScanResultAdapter(context, list);

        vp_scanResult.setAdapter(adapter);

        iv_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isStartAni) {
                    startAnimation();
                } else {
                  stopAnimation();
                }
            }
        });

    }
    private void startAnimation() {
        Animation animation= AnimationUtils.loadAnimation(context, R.anim.rotate);
        LinearInterpolator interpolator = new LinearInterpolator();
        animation.setInterpolator(interpolator);
        animation.setFillAfter(true);
        if (animation != null) {
            isStartAni=true;
            initData();
            iv_scan.startAnimation(animation);
        }
    }

    private void stopAnimation() {
        iv_scan.clearAnimation();
        isStartAni=false;
    }
}
